/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package DBSupport;

/**
 *
 * @author Manuel
 */
import java.sql.*;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

public class QueryExecutor{

	private static final String driverName = "org.postgresql.Driver";
	private static final String dburl = "jdbc:postgresql://localhost:5432/MetricsDB";
	private static final String user = "postgres";
	private static final String passwd = "metrix";

	private static Statement stmt;
	private static Connection con;



	public QueryExecutor(){
                this.connect();
        }

	//+++++++++++++++++++++++METHOD TO EXECUTE INSERTION REQUESTS ON THE DB+++++++++++++++++++++++++++
	public synchronized boolean insert(String query){

		// --------------Executing insertion--------------------
		try {
			stmt = con.createStatement();
			stmt.executeUpdate(query);
		} catch (SQLException e) {
                        System.err.println("Can not connect to DB !!!!!!!!!!!!!!!!!!!!! Problem with: "+query);
                        e.printStackTrace();
                        return false;
		}


		return true;
	}


	//+++++++++++++++++++++++++++METHOD TO EXECUTE QUERY REQUESTS ON THE DB+++++++++++++++++++++

	public synchronized ArrayList<String> query(String query){

		//--------------------Setup connection to db---------------
		String error = "DBERROR";
		ArrayList<String> result = new ArrayList<String>();

		//------------------Executing Query-------------------
		ResultSet rs;
		try {
                        String currentString;
			stmt = con.createStatement();
			rs = stmt.executeQuery(query);
                        int columns = rs.getMetaData().getColumnCount();
			while(rs.next()){
                                currentString ="";
				for(int i = 1; i<=columns;i++){
					if(i != 1)
						currentString += "-";
					currentString += rs.getString(i);
				}
                                result.add(currentString);
			}
		} catch (SQLException e1) {
                        int length = (query.length()>100)? 100:query.length();
                        System.out.println("Can not connect to DB !!!!!!!!!!!!!!!!!!!!! Problem with: "+query);
                        e1.printStackTrace();
			return null;
		}


		return result;
	}




    private boolean connect(){
	try {
            Class.forName(driverName);
            con = DriverManager.getConnection(dburl, user, passwd);
	} catch (SQLException ex) {
            System.err.println("Can not connect to DB");
            Logger.getLogger(QueryExecutor.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex){
            System.err.println("Can not connect to DB!");
            Logger.getLogger(QueryExecutor.class.getName()).log(Level.SEVERE, null, ex);
        }
	return true;
}

    private boolean disconnect(){

	try {
		stmt.close();
		con.close();
	} catch (SQLException e) {
            System.err.println("Can not disconnect from DB!");
	}
	return true;
    }

    /*public void finalize(){
        try {
            this.disconnect();
            super.finalize();

        } catch (Throwable ex) {
        }

    }*/

}

